import { Injectable} from '@angular/core';
import {
  HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse
} from '@angular/common/http';

import { Observable } from 'rxjs';
import {map} from 'rxjs/operators';
import { CommonService } from './common.service';


/** Pass untouched request through to the next request handler. */
@Injectable()
export class NoopInterceptor implements HttpInterceptor {

  constructor(
    private modalService: CommonService,
  ) { }
  intercept(req: HttpRequest<any>, next: HttpHandler):
    Observable<HttpEvent<any>> {
    const request = req.clone({
      setHeaders: {
        'Access-Token-Shop':  '425f8d5566d2f7b9884468fd001a59fc',
        'Access-Token-User':  '410432818ddc33d58c97e98627ca7d00',
        'App-Origin-Type':  '0'
      }
    });
    return next.handle(request).pipe(
      map((event: HttpEvent < any > ) => {
        if (event instanceof HttpResponse) {
          if ( event.body.result === -1) {
            this.modalService.error('错误提示', event.body.errorMsg);
          }
        }
        return event;
      }));
  }
}
